<?php
class Contact extends CustomerController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="contact/index_view";
        $this->_data["loadMenu"]="other/menu_view";
        $this->_data["titlePage"]="Liên hệ";
        
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->load->model("mmodule");
        $tmp=$this->mmodule->listAll();
        if($tmp!=false)
        $this->_data["info_module"]=$tmp;
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtFullName",
                "label"=>"Họ Tên",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $data=array(
                "con_date"=>date("Y-m-d H:i:s"),
                "fullname"=>$this->input->post("txtFullName"),
                "email"=>$this->input->post("txtEmail"),
                "content"=>$this->input->post("txtContent")
            );
            $this->load->model("mcontact");
            $this->mcontact->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm liên hệ thành công.");
            
            //send mail
            $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.gmail.com',
                'smtp_port' => '465',
                'smtp_user' => 'hoangtubanhtrunghoangtuhacma@gmail.com',
                'smtp_pass' => 'hoangtuhoangtu'//Nhớ đánh đúng user và pass nhé
            );
            $this->load->library('email',$config);
            $this->email->set_newline("\r\n");

            $this->email->from('hoangtubanhtrunghoangtuhacma@gmail.com', 'Admin');
            $this->email->to('hoangtubanhtrunghoangtuhacma@gmail.com');
            $this->email->cc('googrand1@gmail.com, ngocthachqn92@gmail.com, nguyenthuyenlong1988@gmail.com');
            $this->email->subject("Thư liên hệ của ".$this->input->post("txtFullName")."-".$this->input->post("txtEmail"));
            $this->email->message($this->input->post("txtContent"));    

            if($this->email->send()){
                echo "Mail đã được gửi cho bạn";
            }
            else
            {
                show_error($this->email->print_debugger());
            }
            
            redirect(base_url()."customer/contact/");
        }
    }
}
?>